package com.servlet;

import com.alibaba.fastjson.JSON;
import com.pojo.PagePojo;
import com.pojo.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author zxl
 * @create :2022/9/28
 */



@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {
    private StudentService studentService = new StudentServiceImpl();

    //新增
    public void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端传过来的参数 然后进行新增
        /*String stuName = request.getParameter("stuName");
        System.out.println("stuName = " + stuName);*/
        //使用获取请求提参数的方式获取数据
        BufferedReader reader = request.getReader();
        //读一行
        String s = reader.readLine();
        //转为java对象才能方便添加
        Student student = JSON.parseObject(s, Student.class);
        //调用Service方法进行新增
        boolean b = studentService.addStudent(student);
        String result = "失败";
        if (b) {
            result = "添加成功";
        }
        //响应给前端
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(result);


    }

    //删除
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        int sid = Integer.parseInt(id);

        //3.封装操作对象
        Student student = new Student();
        student.setId(sid);

        //4.访问StudentService删除
        boolean b = studentService.removeStudent(student);
        String result = "失败";
        //5.判断并跳转
        if (b) {
            //成功  跳转到查询全部的Servlet重新查询一次然后显示

            result = "删除成功";

        }
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(result);


    }


    //查询全部
    public void selectStudentByAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Student> students = studentService.getStudentAll();

        //java对象转为json字符串
        String jsonString = JSON.toJSONString(students);
        System.out.println("jsonString = " + jsonString);

        //将数据响应给前端ajax
        response.setContentType("text/json;charset=utf-8");//设置相应编码
        response.getWriter().write(jsonString);
    }

    //修改
    public void editStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");// 设置响应编码
        //System.out.println("edit");
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Student student = JSON.parseObject(s, Student.class);
        //System.out.println(student);
        boolean b = studentService.modifyStudentById(student);
        if (b == true) {
            response.getWriter().write("success");
        } else {
            response.sendRedirect("修改失败");
        }

    }

    //批量删除
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");// 设置响应编码
        //System.out.println("edit");
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        //System.out.println(s);
        Student student = new Student();
        int[] ints = JSON.parseObject(s, int[].class);
        int count = 0;
        for (int i = 0; i < ints.length; i++) {
            student.setId(ints[i]);//封装对象
            //调用方法
            boolean b = studentService.removeStudent(student);
            if (b == true) {
                count++;
            } else {
                break;
            }
        }
        if (count == ints.length) {
            response.getWriter().write("success");
        } else {
            response.sendRedirect("error.html");
        }
    }

    //分页条件查询
    public void selectConditionStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }

    //分页

    /**
     * public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
     * <p>
     * String currentPage = request.getParameter("currentPage");//当前页
     * String pageSize = request.getParameter("pageSize");//每页显示条数
     * <p>
     * <p>
     * //转换
     * int curr = Integer.parseInt(currentPage);
     * int pas = Integer.parseInt(pageSize);
     * <p>
     * System.out.println(curr);
     * System.out.println(pas);
     * <p>
     * //查询数据
     * PagePojo<Student> studentPagePojo = studentService.selectByPage(curr, pas);
     * <p>
     * String toJSONString = JSON.toJSONString(studentPagePojo);
     * <p>
     * response.setContentType("text/json;charset=utf-8");
     * response.getWriter().write(toJSONString);
     * <p>
     * }
     */
    //带条件的分页查询
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("11");
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/json;charset=utf-8");// 设置响应编码
        //得到路径中的参数
        String current = request.getParameter("currentPage");//当前页
        String size = request.getParameter("pageSize");//每页显示条数

//        //获得请求体参数中的查询条件对象

        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println("s = " + s);
        //将字符串转为java能用的对象
        Student student1 = JSON.parseObject(s,Student.class);

        System.out.println("student1 = " + student1);

        //转换
        int page = Integer.parseInt(current);
        int pageSize = Integer.parseInt(size);


        //查询数据

        PagePojo<Student> studentPagePojo = studentService.selectByPage(page, pageSize, student1);
        System.out.println("studentPagePojo" + studentPagePojo);
        String toJSONString = JSON.toJSONString(studentPagePojo);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(toJSONString);

    }
}

